<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/22
 * Time: 11:49
 */
namespace App\Models;
use App\Services\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\UserTicket;
use App\Exceptions\ApiException;

/**
 * @param $MobileNo string 注册手机号
 * @param $referee string 推荐人
 * @param $unionid string 微信unionid
 * @param $openid string 微信openid
 * @param $is_offline bool 是否是线下
 * @param $introducer_reward int 是否进行奖励
 * @return array|bool
 */
class User extends Model{

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    }

    public function _mediaStars() {
        return $this->hasMany('App\Models\MediaStar','uid');
    }

    public function register($MobileNo,$unionid,$openid,$password, $registration_id)
    {
        //根据openid获取授权登录微信用户信息
//        $weixin_userinfo = DB::table('wechat_users')->where('openid',$openid)->first();
        $name = '用户'.random_num(13);
        $userInfo = [
            'mobile' => $MobileNo,
            'name' => $name,
            'password' => md5($password),
            'status' => 1,
            'avatar'=> '',
            'created_at'    =>  date('Y-m-d H:i:s'),
            'updated_at'    =>  date('Y-m-d H:i:s'),
            'registration_id'    =>  $registration_id
        ];
//        Log::info(__FILE__, 'register', $userInfo);
        if($openid){
            $userInfo['openid'] = $openid;
        }
        $uid = DB::table('users')->insertGetId($userInfo);//插入成功返回id值
        if ($uid) {
            //注册成功将UID更新到user_tags表
            $tags_data =[
                'uid' => $uid,
            ];
            DB::table('user_tags')->insert($tags_data);
            //生成ticket入库
            $ticket = get_ticket($uid);
            if ($ticket) {
                return ['ticket' => $ticket, 'uid' => $uid];
            }
        }

        $invites = D('friend_invite')->where('mobile', $MobileNo)->get(['uid']);
        $date = time2date();
        $data = [];
        if ($invites) {
            foreach ($invites as $invite) {
                $_data = [$uid, $invite->uid];
                sort($_data);
                $data[] = ['uid' => $_data[0], 'friend_uid' => $_data[1], 'status' => 1, 'created_at' => $date, 'updated_at' => $date];
            }
            D('friend')->insert($data);
        }

      //  DB::rollBack();
        return false;
    }

    /** 
     *  获取Vip身份字符串
     */
    static function getVipStr($vip_level)
    {
    
            switch ($vip_level) {
                case 0:
                    return '普通用户';
                break;                
                case 1:
                    return '月度用户';
                break;
                case 2:
                    return '年度用户';
                break;           
                default:
                    return '';
                break;
            }
    
    }

    /**
     * @param $ticket
     * @return mixed
     * @throws ApiException
     */
    static function getUserInfo($ticket)
    {
        if (!$ticket) {
            throw new ApiException('缺少参数ticket',0);
        }
        $user_ticket = UserTicket::where([ ['ticket','=',$ticket],['status','=',1] ])->first();
        if(!$user_ticket){
            throw new ApiException('用户未登录', 1000);
        }
        return $user_ticket->_user;
    }

    public function _medias()
    {
        return $this->hasMany('App\Models\Media','uid')->where([['media.status', 1], ['media.is_delete', '=', 0]]);
    }

    public function _mediaComments()
    {
        return $this->hasMany('App\Models\MediaComment','uid');
    }

        /**
     * 原来用户
     *
     * @return void
     * @date 
     * @version v1.0
     */
   public function _fans_friends(){
        return $this->hasMany('App\Models\Friend','uid');
    }

    /**
     * 添加好友用户
     *
     * @return void
     * @date 
     * @version v1.0
     */
    public function _follow_friends()
    {
        return $this->hasMany('App\Models\Friend','friend_id');
    }
}